package com.ren.service.core.rest;

import com.ren.common.result.R;
import com.ren.common.result.ResponseEnum;
import com.ren.service.core.model.vo.DictVO;
import com.ren.service.core.service.IDictService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author RenGaoshuai
 * @date 2023/8/7 17:18
 * @description
 */
@RestController
@RequestMapping("/admin/core/dict")
@Slf4j
public class DictController {

    @Autowired
    private IDictService dictService;

    @PostMapping("/import")
    public R importDict(@RequestParam("file") MultipartFile file) {
        try {
            dictService.importData(file.getInputStream());
            return R.ok().message("批量导入成功");
        } catch (IOException e) {
            log.error("文件上传异常：原因：", e);
            return R.setResult(ResponseEnum.UPLOAD_ERROR);
        }

    }

    @PostMapping("/export")
    public R exportDict(HttpServletResponse response) {

        String fileName = dictService.exportData(response);
        return R.ok().message("批量导入成功");

    }

    @ApiOperation("根据上级id获取子节点数据列表")
    @GetMapping("/listByParentId/{parentId}")
    public R listByParentId(@PathVariable Long parentId) {
        List<DictVO> dictList = dictService.listByParentId(parentId);
        return R.ok().data("list", dictList);
    }
}
